from flask import render_template, url_for, request, flash, Blueprint
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
web = Blueprint('web', __name__, template_folder='templates')


class User(db.Model):

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(50), unique=True)
    password = db.Column(db.String(128))
    content = db.Column(db.String(1024))


@web.route("/")
def home():
    """
    打开网站第一页
    :return:
    """
    return render_template('home.html')


@web.route("/login/", methods=['GET', 'POST'])
def login():
    if request.method == "POST":
        return url_for(home)
    else:
        error = ("账号或者密码输入错误")
    return render_template('login.html', error=error)


@web.route("/regist/", methods= ['GET', 'POST'])
def regist():
    if request.method == 'POST':
        username = request.form.get('username')
        # 匹配两个密码是否相同,添加到数据库
        if request.form.get('password') == request.form.get('repassword'):
            user = User(username=username, password = request.form.get('password'))
            db.session.add(user)
            db.session.commit()
    else:
        flash("两次密码不一致")
        return render_template("register.html")


@web.route('/exit/')
def exit():
    pass


